/**
 * Initializes the autocomplete widget from jQuery UI.
 * Uses ajax for getting the data from the server and showing it
 * to the user.
 * 
 * @author cristian.daraban
 * @version 1.0
 * 
 * @param target - Requires a jQuery selector
 * @param type - Requires the method for transimition like GET, POST
 * @param url - Requires the url to a server page which will handle the data transmision
 * 
 * @example
 * initAutocomplete($('.selector'),'GET', '/page.php');
 */
var initAutocomplete = function(target, type, url) {
	target.autocomplete({
		delay : 500,
		minLength : 2,
		source : function(request, response) {
			$.ajax({
				url : url,
				dataType : "json",
				data : {
					term : request.term
				},
				success : function(data) {
					console.log(data);
					response($.map(data, function(item) {
						return {
							label : item,
							value : item
						}
					}));
				}
			});
		},
		select : function(event, ui) {
			/*
			 * console.log("Event: Select"); console.log(ui.item ? "Selected: " +
			 * ui.item.label : "Nothing selected, input was " + this.value);
			 */
		},
		open : function(event, ui) {
			$("#ui-id-1").zIndex(1000000).show();
		}
	});

};

$(document)
		.ready(
				function() {
					initAutocomplete($('#destination'), 'GET', 'autocomplete');
					/* Add Rooms */
					$('#room1-adult-count').keypress(function(event) {
						var e = event.keyCode ? event.keyCode : event.which;
						console.log(e);
						if (e != 8) {
							if (e < 53 && e > 48) {
								$('#room1-adult-count').val(e - 48);
							} else {
								event.preventDefault();
							}
						}
					});

					$('#room1-children-count').keypress(function(event) {
						var e = event.keyCode ? event.keyCode : event.which;
						console.log("Children");
						console.log(event.keyCode);
						console.log(event.which);
						if (e != 8) {
							if (e < 52 && e > 47) {
								$('#room1-children-count').val(e - 48);
								updateAgeFields(1, e);

							} else {
								event.preventDefault();
							}
						}
					});

					$('#room1-child-age1').keypress(function(event) {
						var e = event.keyCode ? event.keyCode : event.which;
						if (e != 8) {
							if (e < 58 && e > 47) {
							} else {
								event.preventDefault();
							}
						}
					});
					$('#room1-child-age2').keypress(function(event) {
						var e = event.keyCode ? event.keyCode : event.which;
						if (e != 8) {
							if (e < 58 && e > 47) {
							} else {
								event.preventDefault();
							}
						}
					});
					$('#room1-child-age3').keypress(function(event) {
						var e = event.keyCode ? event.keyCode : event.which;
						if (e != 8) {
							if (e < 58 && e > 47) {
							} else {
								event.preventDefault();
							}
						}
					});

					$('#search-widget-start')
							.datepicker(
									{
										dateFormat : "dd/mm/yy",
										minDate : "0d",
										maxDate : "+499d",
										onSelect : function(selectedDate) {
											if ($('#search-widget-start')
													.datepicker('getDate') != null) {
												var currentDate = $(
														'#search-widget-start')
														.datepicker('getDate'), maxDate;
												currentDate.setDate(currentDate
														.getDate() + 1);

												$('#search-widget-end')
														.datepicker('option',
																'minDate',
																currentDate);
												maxDate = $(
														'#search-widget-start')
														.datepicker('getDate');
												maxDate.setDate(maxDate
														.getDate() + 27);
												$('#search-widget-end')
														.datepicker('option',
																'maxDate',
																maxDate);
											}
										},
										onClose : function(selectedDate) {
											var currentDate = $(
													'#search-widget-start')
													.datepicker('getDate'), length = $(
													'#search-widget-start')
													.val().length;
											if (currentDate == null
													|| length != 10
													|| !isDate(
															$(
																	'#search-widget-start')
																	.val(),
															'dd/mm/yy')) {
												$('#search-widget-start')
														.datepicker('setDate',
																"0d");
											}
										}

									});
					$('#search-widget-end')
							.datepicker(
									{
										dateFormat : "dd/mm/yy",
										minDate : "+1d",
										maxDate : "+5",
										beforeShow : function(selectedDate) {
											if ($('#search-widget-start')
													.datepicker('getDate') != null) {
												var currentDate = $(
														'#search-widget-start')
														.datepicker('getDate'), maxDate;
												currentDate.setDate(currentDate
														.getDate() + 1);
												$('#search-widget-end')
														.datepicker('option',
																'minDate',
																currentDate);
												maxDate = $(
														'#search-widget-start')
														.datepicker('getDate');
												maxDate.setDate(maxDate
														.getDate() + 27);
												$('#search-widget-end')
														.datepicker('option',
																'maxDate',
																maxDate);
											}
										},
										onClose : function(selectedDate) {
											var startDate = $(
													'#search-widget-start')
													.datepicker('getDate'), endDate = $(
													'#search-widget-end')
													.datepicker('getDate'), maxDate = $(
													"#search-widget-end")
													.datepicker("option",
															"maxDate"), length = $(
													'#search-widget-end').val().length;
											if (!isDate($('#search-widget-end')
													.val(), 'dd/mm/yy')
													|| (startDate > endDate)
													|| (endDate > maxDate)
													|| (length != 10)) {
												$('#search-widget-end')
														.datepicker('setDate',
																startDate);
												console.log($(
														'#search-widget-end')
														.val());
												console.log(startDate);
											}
										}
									});

					/* Default settings for all datepickers */
					$.datepicker.formatDate("dd/mm/yy", new Date());
					$('#search-widget-start').datepicker('setDate', '+1d');
					$('#search-widget-end').datepicker('setDate', '+2d');
				});
var addRoom = function() {
	var count = +$('#hidden-room-count').val(), tabs = $('#search-widget-tabs'), tabContent = $('#search-widget-tab-content'), content = $(
			'#search-widget-tab-content div.tab-pane').first().clone(), uniqIdForRoomLink = "roomSelection"
			+ (count + 1), uniqId = "search-widget-room-tab" + (count + 1), noChild = 0, noAdults = 2, age = 7, temp, max = 8;
	// console.log($('#hidden-room-count'));
	// console.log(count);
	// console.log($('div.tab-content div.tab-pane').length);
	count = $('div.tab-content div.tab-pane').length;
	if (count + 1 <= 8) {
		/* make everything to be default */
		temp = content.removeClass('active').attr('id', uniqId);
		temp.find('#room1-adult-count').attr('id',
				'room' + (count + 1) + '-adult-count').attr('value', noAdults)
				.attr('name', 'rooms[' + (count) + '].adultsCount');
		temp.find('#room1-children-count').attr('id',
				'room' + (count + 1) + '-children-count').attr('name',
				'rooms[' + (count) + '].childrenCount').attr('value', noChild);
		temp.find('#room1-child-age1').attr('id',
				'room' + (count + 1) + '-child-age1').attr('value', age).attr(
				'name', 'rooms[' + (count) + '].childAges');
		temp.find('#room1-child-age2').attr('id',
				'room' + (count + 1) + '-child-age2').attr('value', age).attr(
				'name', 'rooms[' + (count) + '].childAges');
		temp.find('#room1-child-age3').attr('id',
				'room' + (count + 1) + '-child-age3').attr('value', age).attr(
				'name', 'rooms[' + (count) + '].childAges');
		temp.find('#room1-child-age-text').attr('id',
				'room' + (count + 1) + '-child-age-text');
		/* add to the tab content a new room */
		temp.appendTo(tabContent);

		/* add a new room in the tab area */
		tabs.append('<li><a id="' + uniqIdForRoomLink + '" href="#' + uniqId
				+ '" data-toggle="tab">Room ' + (count + 1) + '</a></li>');

		/* update the room count */
		updateAgeFields((count + 1));
		$('#hidden-room-count').val(count + 1);

		$('#room' + (count + 1) + '-children-count').keypress(function(event) {
			var e = event.keyCode ? event.keyCode : event.which;
			console.log(e);
			if (e != 8) {
				if (e < 52 && e > 47) {
					$('#room' + (count + 1) + '-children-count').val(e - 48);
					updateAgeFields(count + 1, e);
				} else {
					event.preventDefault();
				}
			}
		});

		$('#room' + (count + 1) + '-adult-count').keypress(function(event) {
			var e = event.keyCode ? event.keyCode : event.which;
			console.log(e);
			if (e != 8) {
				if (e < 53 && e > 48) {
					$('#room' + (count + 1) + '-adult-count').val(e - 48);
				} else {
					event.preventDefault();
				}
			}
		});
		if ($('#search-widget-remove-room').hasClass('disabled')) {
			$('#search-widget-remove-room').removeClass('disabled');
		}

		$('#room' + (count + 1) + '-child-age1').keypress(function(event) {
			var e = event.keyCode ? event.keyCode : event.which;
			console.log(e);
			if (e != 8) {
				if (e < 58 && e > 47) {
				} else {
					event.preventDefault();
				}
			}
		});
		$('#room' + (count + 1) + '-child-age2').keypress(function(event) {
			var e = event.keyCode ? event.keyCode : event.which;
			console.log(e);
			if (e != 8) {
				if (e < 58 && e > 47) {
				} else {
					event.preventDefault();
				}
			}
		});
		$('#room' + (count + 1) + '-child-age3').keypress(function(event) {
			var e = event.keyCode ? event.keyCode : event.which;
			console.log(e);
			if (e != 8) {
				if (e < 58 && e > 47) {
				} else {
					event.preventDefault();
				}
			}
		});

		updateAgeFields((count + 1), null);
	}

}, removeRoom = function() {
	var count = +$('#hidden-room-count').val(), tabs = $(
			'#search-widget-tabs li').last(), tabContent = $(
			'#search-widget-tab-content div.tab-pane').last();
	if (count > 1) {
		if (tabs.hasClass('active')) {
			$('#search-widget-tabs li').first().addClass('active');
			$('#search-widget-tab-content div.tab-pane').first().addClass(
					'active');
		}
		tabs.remove();
		tabContent.remove();
		count -= 1;
		$('#hidden-room-count').val(count);
		if (count === 1) {
			if (!$('#search-widget-remove-room').hasClass('disabled')) {
				$('#search-widget-remove-room').addClass('disabled');
			}
		}

	}

}, updateAgeFields = function(id, code) {
	var childrenCount = +$('#room' + id + '-children-count').val(), age1 = $('#room'
			+ id + '-child-age1'), age2 = $('#room' + id + '-child-age2'), age3 = $('#room'
			+ id + '-child-age3'), ageText = $('#room' + id + '-child-age-text');
	if (code != undefined) {
		childrenCount = code - 48;
	}
	switch (childrenCount) {
	case 1:
		ageText.removeClass('hidden');
		age1.removeClass('hidden');
		age2.addClass('hidden');
		age3.addClass('hidden');

		break;
	case 2:
		ageText.removeClass('hidden');
		age1.removeClass('hidden');
		age2.removeClass('hidden');
		age3.addClass('hidden');
		break;
	case 3:
		ageText.removeClass('hidden');
		age1.removeClass('hidden');
		age2.removeClass('hidden');
		age3.removeClass('hidden');
		break;
	default:
		ageText.addClass('hidden');
		age1.addClass('hidden');
		age2.addClass('hidden');
		age3.addClass('hidden');
		break;
	}
}, updateEmptyField = function(id, temp) {
	if (id.value.length == 0) {
		id.value = temp;
	}
}, updateValueField = function(field) {
	if (field.value > 17) {
		field.value = 17;
	}
};